package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;

/**
 * 公告控制器 - 处理系统公告管理相关的HTTP请求
 * 包括公告的增删改查、状态管理、前台展示等功能
 */
@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/notices")  // 公告API路径前缀
@CrossOrigin  // 允许跨域访问
@Tag(name = "公告管理", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")  // Swagger API分组
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PutMapping("/update")
    @Operation(summary = "更新公告")
    public Result updateNotice(@RequestBody Notice notice) {
        boolean update = noticeService.updateById(notice);
        if (update) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @PutMapping("/toggle/{id}")
    @Operation(summary = "切换公告状态")
    public Result toggleNotice(@PathVariable long id, @RequestParam boolean isActive) {
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        noticeLambdaUpdateWrapper.set(Notice::getIsActive,isActive).set(Notice::getUpdateTime,new Date());
        noticeLambdaUpdateWrapper.eq(Notice::getId, id);
        boolean update = noticeService.update(noticeLambdaUpdateWrapper);
        if (update) {
            return Result.success("切换成功");
        } else {
            return Result.error("切换失败");
        }
    }

    @PostMapping("/add")
    @Operation(summary = "发布公告")
    public Result addNotice(@RequestBody Notice notice) {
        noticeService.save(notice);
        return Result.success("添加成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取公告")
    public Result<Notice> getNoticeById(@PathVariable long id) {
        Notice byId = noticeService.getById(id);
        return Result.success(byId);
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有公告")
    public Result<List<Notice>> getAllNotices() {
        List<Notice> list = noticeService.list();
        return Result.success(list);
    }

    @GetMapping("/active")
    @Operation(summary = "获取启用的公告")
    public Result<List<Notice>> getActiveNotices() {
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.eq("is_active",true);
        List<Notice> list = noticeService.list(noticeQueryWrapper);
        return Result.success(list);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据id删除公告")
    public Result deleteNotice(@PathVariable long id) {
        boolean b = noticeService.removeById(id);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新的公告")
    public Result<List<Notice>> getLatestNotice(@RequestParam(defaultValue = "5") int limit) {
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.orderByDesc("update_time").last("limit " + limit);
        List<Notice> list = noticeService.list(noticeQueryWrapper);
        return Result.success(list);
    }
} 